package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Comment;
import pojo.User;
import service.AdminCommentService;
import service.AdminUserService;
import service.UserCommentService;
import util.CommonsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserComment {
    @Autowired
    private UserCommentService userCommentService;
    @Autowired
    private AdminCommentService adminCommentService;
    @Autowired
    private AdminUserService adminUserService;
    @RequestMapping("/toAddC")
    public String initAddC(Model model){
        List<Comment> c = adminCommentService.sAC();
        model.addAttribute("c",c);
        return "userAddComment";}

    @RequestMapping("/addCom")
    public String addComment(ModelMap map, Model model, HttpSession session, HttpServletRequest request){
        int uid = CommonsUtils.getUserId(session);
        List<User> userList = adminUserService.sOU(uid);
        User user=userList.get(0);
        String uname = user.getUname();
        String title = request.getParameter("ctitle");
        String content = request.getParameter("ccontent");
        Comment comment=new Comment();
        comment.setCtitle(title);
        comment.setCcontent(content);
        comment.setUname(uname);
        comment.setUid(uid);
        int a = userCommentService.aCT(comment);
        if (a>0){
            List<Comment> c = adminCommentService.sAC();
            model.addAttribute("c",c);
            map.addAttribute("comment",comment);
            model.addAttribute("msg","提交成功");
            return "userAddComment";
        }
        List<Comment> c = adminCommentService.sAC();
        model.addAttribute("c",c);
        model.addAttribute("msg","提交失败");
        return "userAddComment";
    }
}
